﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using YEngineLib;
using YEngineLib.Animation;

using Spyfall.Managers;

namespace Spyfall.Objects
{
    public class Enemy : Object
    {
        public Texture2D texture;
        List<Animation> enemyAnimationList;
        Animation animation;
        //States.GameState counter;
        public Vector2 origin;
        public Vector2 position;
        public Vector2 velocity;

        bool moveUp = false;
        bool moveDown = false;

        float[] pos = { 0, 10, 30, 50 };
        int lastSpd = -1;

        public Enemy()
        {
            //1280 x 720
            ObjectID = Object_Type.ENEMY;
            position.X = YEngine.Graphics.ScreenViewport.Width / 2;
            position.Y = YEngine.Graphics.ScreenViewport.Height + pos[3];

            //Animation Stuff start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            texture = YEngine.Game.Content.Load<Texture2D>("Graphics/dive");
            enemyAnimationList = new List<Animation>();
            animation = new Animation();

            animation.Initialize(texture, position, 150, 196, 6, 0.1f, Color.White, 1.0f, true);
            enemyAnimationList.Add(animation);

            ColRect = new Rectangle(int.Parse(position.X.ToString()), int.Parse(position.Y.ToString()), texture.Bounds.Left, texture.Bounds.Top);
        }

        public override void Update(float deltaTime)
        {
            if (lastSpd == -1)
                lastSpd = LevelManager.GetInstance.SpeedUpNum;

            //ANimation Stuff start
            for (int i = enemyAnimationList.Count - 1; i >= 0; i--)
            {
                enemyAnimationList[i].Update(deltaTime);
                if (enemyAnimationList[i].Active == false)
                    enemyAnimationList.RemoveAt(i);
            }

            if (lastSpd > LevelManager.GetInstance.SpeedUpNum)
                moveUp = true;

            if (lastSpd < LevelManager.GetInstance.SpeedUpNum)
                moveDown = true;

            if (moveUp)
            {
                if (YEngine.Graphics.ScreenViewport.Height + pos[LevelManager.GetInstance.SpeedUpNum] < position.Y)
                    position.Y -= pos[LevelManager.GetInstance.SpeedUpNum] * deltaTime;
                else
                {
                    position.Y = YEngine.Graphics.ScreenViewport.Height + pos[LevelManager.GetInstance.SpeedUpNum];
                    moveUp = false;
                    lastSpd = LevelManager.GetInstance.SpeedUpNum;
                }
            }

            if (moveDown)
            {
                if (YEngine.Graphics.ScreenViewport.Height + pos[LevelManager.GetInstance.SpeedUpNum] > position.Y)
                    position.Y += pos[LevelManager.GetInstance.SpeedUpNum] * deltaTime;
                else
                {
                    position.Y = YEngine.Graphics.ScreenViewport.Height + pos[LevelManager.GetInstance.SpeedUpNum];
                    moveUp = false;
                    lastSpd = LevelManager.GetInstance.SpeedUpNum;
                }
            }

            //Animation Stuff end
            animation.Position = position;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            //Animation stuff start
            for (int i = 0; i < enemyAnimationList.Count; i++)
            {
                enemyAnimationList[i].Draw(spriteBatch);
            }
            //animation stuff end
        }

        public override bool Collide(Object tar)
        {
            return ColRect.Contains(tar.ColRect);
        }

    }
}
